jQuery(function () {
	//获取显示页面数据 服务器分页
    var serverData = {};
    serverData.key =''
    var serverTable;
    jQuery(document).ready(function () {
        serverTable = jQuery('#replaceDeviceTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/replacementDevice/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
  	                {
  	                	data: null
  	                },
  	                {
	                    data: null,
	                    defaultContent: "",
	                    render: function (data, type, full, meta) {
	                        var id=data.id
	                        var operate ='<input type="checkbox" class="checkchildReplace">';
	                        return operate;
	                    }
  	                },
  	                {
  	                    data: 'deviceName',
		                render:function(data,type,full,meta){
		                	if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
							}
						}
  	                },
  	                {
  	                    data: 'type',
		                render:function(data,type,full,meta){
		                	if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
							}
						}
  	                },
  	                {
  	                    data: 'material',
		                render:function(data,type,full,meta){
		                	if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
							}
						}
  	                },
  	                {
  	                    data: 'unit'
  	                },
  	                {
  	                    data: 'amount'
  	                },
  	                {
  	                    data: 'hostType'
  	                },
  	                {
  	                    data: 'attachNumber',
  	                    render: function (data, type, full, meta) {
  	                    	var dOut;
  	                    	if(data==null){
  	                    		dOut='<td class="center" ><span class="badge label-pill label-primary">'+0+'</span></td>'
  	                    	}else{
  	                    		dOut='<td class="center" ><span class="badge label-pill label-primary">'+data+'</span></td>'
  	                    	}
  	                    	return dOut;
  	                    }
  	                    
  	                }  
  	               ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                } 
            }
        });
        //删除备品备件设备
        jQuery('.delBtn').on('click', function () {
            var selects=getSelect("#replaceDeviceTable",1);
            var url=window.rootPath+"/ui/replacementDevice/delReplacementDevice";
            delSelect(selects,url,serverTable)
        });

        //添加销售人员属性，弹窗清空
    	 jQuery('.addBtn').on('click', function () {
    		jQuery('#id').val('');
    		jQuery('#deviceName').val('');
    		jQuery('#type').val('');
    		jQuery('#material').val('');
    		jQuery('#unit').val('');
    		jQuery('#amount').val('');
    		jQuery('#price').val('');
    		jQuery('#discount').val('');
    		jQuery('#hostType').val('');
    		jQuery('#replacementDeviceModal').modal('show');
    	});

         //编辑成套设备信息
        jQuery('.editBtn').on('click', function () {
            var selects=getSelect("#replaceDeviceTable",1);
            if(selects.length>1){
                $.springLemonDialog({
                    content: '只能编辑一条记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }
            else if(selects.length==0){
                $.springLemonDialog({
                    content: '请选择要编辑的记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }else if(selects.length==1) {
                jQuery('#id').val(selects[0].id);
	    		jQuery('#deviceName').val(selects[0].deviceName);
	    		jQuery('#attachNumber').val(selects[0].attachNumber);
	    		jQuery('#type').val(selects[0].type);
	    		jQuery('#material').val(selects[0].material);
	    		jQuery('#unit').val(selects[0].unit);
	    		jQuery('#amount').val(selects[0].amount);
	    		jQuery('#price').val(selects[0].price);
	    		jQuery('#discount').val(selects[0].discount);
	    		jQuery('#hostType').val(selects[0].hostType);
	    		jQuery('#replacementDeviceModal').modal('show');
            };
        });
        //上传附件
        jQuery('.upLoadBtn').on('click', function () {
            var selects=getSelect("#replaceDeviceTable",1);
            if(selects.length>1){
                $.springLemonDialog({
                    content: '只能对一条记录上传文档',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }
            else if(selects.length==0){
                $.springLemonDialog({
                    content: '请选择要上传文档的记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }else if(selects.length==1) {
            	var deviceId=selects[0].id;
            	var fileType=selects[0].deviceName
            	var url='/ui/replacementDevice';
            	fileUpload(deviceId,fileType,serverTable); 	 	
            };
        });
       
        
        //下载附件
        jQuery('.downLoadBtn').on('click', function () {
            var selects=getSelect("#replaceDeviceTable",1);
            if(selects.length>1){
                $.springLemonDialog({
                    content: '只能对一条记录下载文档',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }
            else if(selects.length==0){
                $.springLemonDialog({
                    content: '请选择要下载的记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }else if(selects.length==1) {
            	var deviceId=selects[0].id;
            	var fileType=selects[0].deviceName
            	filedown(deviceId,fileType,'1');
            };
        });

        //列表中的全选
        $("#checkAllReplace").click(function(){
            var check = $(this).prop("checked");
            $(".checkchildReplace").prop("checked", check);
        });
        
        //验证备件名、规格号、材料是否存在
//        jQuery("#deviceName").blur(function(){
//        	validation();
//        })
//        jQuery("#type").blur(function(){
//        	validation();
//        })
//        jQuery("#material").blur(function(){
//        	validation();
//        })
//        //验证方法
//        var validation=function(){
//        	var deviceName=jQuery("#deviceName").val();
//        	var standardType=jQuery("#type").val();//规格型号
//        	var material=jQuery("#material").val();
//        	var d={
//        				"deviceName":deviceName,
//        				"standardType":standardType,
//        				"material":material
//        		   }
//        	var url=window.rootPath+"/ui/replacementDevice/replacementDeviceValidate";
//        		jQuery.ajax({
//        			url:  encodeURI(url),
//        			type: 'get',
//        			data:d,
//        			dataType: 'json',
//        			success:function(data){
//        				if(data.code>0){
//        					message("备品名、信号和材料重复");
//        				}
//        			}
//        		});
//        }
        
        //添加/编辑销售人员属性，保存
    	var $btn = jQuery('.saveBtn').on('click', function () {
            if(jQuery("#replacementDeviceEditForm") && !jQuery("#replacementDeviceEditForm").Validform({tiptype:3, showAllError:true}).check(false)){
                return;
            };
            var isId=jQuery("#id").val();
            var deviceName=jQuery('#deviceName').val();
            var attachNumber = jQuery('#attachNumber').val();
         	var standardType = jQuery('#type').val();//规格型号
         	var material=jQuery('#material').val();
         	var unit = jQuery('#unit').val();
     		var amount = jQuery('#amount').val();
     		var price = jQuery('#price').val();
     		var discount = jQuery('#discount').val();
     		var hostType = jQuery('#hostType').val();
     		
     		
     		//验证备件名、规格号、材料是否重复
     		var d={
     				"id":isId,
     				"deviceName":deviceName,
     				"standardType":standardType,
     				"material":material
     		}
     		var url=window.rootPath+"/ui/replacementDevice/replacementDeviceValidate";
     		jQuery.ajax({
    			url:  encodeURI(url),
    			type: 'get',
    			data:d,
    			dataType: 'json',
    			success:function(data){
    				if(data.code>0){
    					message("备品名、信号和材料重复");
    				}else{
    					//判断是编辑还是新增
    		            if(isId == ""){
    		            	 var api = window.rootPath + "/ui/replacementDevice/add";
    			             var data = {
    			                	"deviceName": deviceName,
    			                	"attachNumber":attachNumber,
    			                    "type": standardType,
    			                    "material":material,
    			                    "unit": unit,
    			                    "amount": amount,
    			                    "price": price,
    			                    "discount": discount,
    			                    "hostType": hostType
    			                };
    			              jQuery(".btn").attr("disabled", true);
    			              jQuery.ajax({
    			                    type: 'post',
    			                    url: api,
    			                    data: JSON.stringify(data),
    			                    dataType: 'json',
    			                    contentType: 'application/json;charset=utf-8',
    			                    success: function (data) {
    			                    	jQuery(".btn").attr("disabled", false);
    			                    	jQuery('#replacementDeviceModal').modal('hide');
    			                    	serverData.key ='';
    			                        serverTable.draw();
    			                    }
    			                });
    		            }
    		            else{
    		            	var data={
    		                        "id":isId,
    		                        "deviceName": deviceName,
    		                        "attachNumber":attachNumber,
    		                        "type": standardType,
    		                        "material":material,
    		                        "unit": unit,
    		                        "amount": amount,
    		                        "price": price,
    		                        "discount": discount,
    		                        "hostType": hostType
    		                    };
    		                    var url=window.rootPath+'/ui/replacementDevice/';
    		                    jQuery(".btn").attr("disabled", true);
    		                    jQuery.ajax({
    		                        url:encodeURI(url),
    		                        type:'patch',
    		                        data:JSON.stringify(data),
    		                        dataType:'json',
    		                        contentType:'application/json;charset=utf-8',
    		                        success: function () {
    		                        	jQuery(".btn").attr("disabled", false);
    		                            jQuery('#replacementDeviceModal').modal('hide');
    		                            serverTable.draw(false);
    		                        }

    		                    });
    		            }
    				}
    			}
    		});
     		
        });
        
    	 
    	//查询
        jQuery('.searchBtn').on('click',function(){
            serverData.key=jQuery('#keyword').val();
            serverTable.draw();
        })
        //撤销查询
        jQuery('.resetBtn').on("click",function()
        {
            jQuery('#keyword').val('');
            serverData.key ='';
            serverTable.draw();
        });
        
        //EXCEL导入
        jQuery('.btnImport').on("click",function()
        {
           fileUpload1();
        });
        /**
    	 * 导入文件上传插件
    	 */
    	var fileUpload1 = function() {
    		var root = window.rootPath;
    		WebFilesUploader(
    				{
    					_webuploader : {
    						server : root + '/ui/replacementDevice/import',
    						chunked:false		
    					}
    				
    				}, function(jsons) {    				
    					 window.location.href=root+'/ui/replacementDevice';
    				});
    	};
        
    	///excel导出
    	jQuery('.btnExport').on("click",function() {
    		var selects=getSelect("#replaceDeviceTable",1);
    		if(selects.length>0){
    			var key=selects[0].id;
    			for(var i=1;i<selects.length;i++){
    				   key += ',' + selects[i].id;  
    			 }
    			   var url=window.rootPath+ "/ui/replacementDevice/exportSelects/"+key;
    			   window.location = url
    			
    		}else{
    			var key = jQuery('#keyword').val();
    			if (!key)
    				key = 'null';
    			var url = window.rootPath+ "/ui/replacementDevice/export/" + key;
    			window.location = url;
    		}
			
		});
        
        
        
        
        
        
        

    });
})
	

    
    
    








